#     TextLocutor
#     Copyright (C) 2009  Jose Ignacio Naranjo Hernandez (joseignacio.naranjo@gmail.com)

#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU Affero General Public License as
#     published by the Free Software Foundation, either version 3 of the
#     License, or (at your option) any later version.

#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU Affero General Public License for more details.

#     You should have received a copy of the GNU Affero General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.

from google.appengine.ext import db
from core.models import CoreUser

class SmsFixture(db.Model):
    coreuser = db.ReferenceProperty(CoreUser,
                                    required=True)
    active = db.BooleanProperty(default=True) # Allow the user reject the sms, and preserve them just for the other plugins
    users_allowed = db.ListProperty(db.Key)
    users_waiting = db.ListProperty(db.Key)

    @staticmethod
    def get_or_insert_by_coreuser(coreuser):
        smsuser = SmsFixture.all().filter('coreuser =', coreuser).get()
        if not smsuser:
            smsuser = SmsFixture(coreuser=coreuser)
            smsuser.put()
        return smsuser

    @staticmethod
    def get_by_coreuser(coreuser):
        return SmsFixture.all().filter('coreuser =', coreuser).get()

def init_user_db(coreuser):
    SmsFixture.get_or_insert_by_coreuser(coreuser)

def del_user_db(coreuser):
    smsuser = SmsFixture.get_by_coreuser(coreuser)
    
    if smsuser:
        smsuser.delete()
